package com.ruoyi.find.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.find.domain.FurnitureWorkOrder;
import com.ruoyi.find.service.IFurnitureWorkOrderService;

/**
 * 家具工单Controller
 * 
 * @author ruoyi
 * @date 2025-05-24
 */
@Tag(name = "家具工单管理", description = "家具工单相关接口")
@RestController
@RequestMapping("/find/furnitureWorkOrder")
public class FurnitureWorkOrderController extends BaseController
{
    @Autowired
    private IFurnitureWorkOrderService furnitureWorkOrderService;

    /**
     * 查询家具工单列表
     */
    @Operation(summary = "查询家具工单列表", description = "获取所有家具工单的分页列表")
    @PreAuthorize("@ss.hasPermi('find:workOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(FurnitureWorkOrder furnitureWorkOrder)
    {
        startPage();
        List<FurnitureWorkOrder> list = furnitureWorkOrderService.selectFurnitureWorkOrderList(furnitureWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出家具工单列表
     */
    @Operation(summary = "导出家具工单列表", description = "将家具工单列表导出为Excel文件")
    @PreAuthorize("@ss.hasPermi('find:workOrder:export')")
    @Log(title = "家具工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FurnitureWorkOrder furnitureWorkOrder)
    {
        List<FurnitureWorkOrder> list = furnitureWorkOrderService.selectFurnitureWorkOrderList(furnitureWorkOrder);
        ExcelUtil<FurnitureWorkOrder> util = new ExcelUtil<FurnitureWorkOrder>(FurnitureWorkOrder.class);
        util.exportExcel(response, list, "家具工单数据");
    }

    /**
     * 获取家具工单详细信息
     */
    @Operation(summary = "获取家具工单详细信息", description = "根据工单ID获取家具工单详细信息")
    @PreAuthorize("@ss.hasPermi('find:workOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(furnitureWorkOrderService.selectFurnitureWorkOrderByOrderId(orderId));
    }

    /**
     * 根据工单编号查询工单
     */
    @Operation(summary = "根据工单编号查询工单", description = "通过工单编号获取家具工单信息")
    @GetMapping(value = "/byCode/{orderCode}")
    public AjaxResult getByOrderCode(@PathVariable("orderCode") String orderCode)
    {
        return AjaxResult.success(furnitureWorkOrderService.selectFurnitureWorkOrderByOrderCode(orderCode));
    }

    /**
     * 新增家具工单
     */
    @Operation(summary = "新增家具工单", description = "创建新的家具工单")
    @PreAuthorize("@ss.hasPermi('find:workOrder:add')")
    @Log(title = "家具工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FurnitureWorkOrder furnitureWorkOrder)
    {
        try {
            // 设置买家用户信息
            furnitureWorkOrder.setBuyerUserId(SecurityUtils.getUserId());
            furnitureWorkOrder.setBuyerUserName(SecurityUtils.getUsername());
            return toAjax(furnitureWorkOrderService.createWorkOrder(furnitureWorkOrder));
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改家具工单
     */
    @Operation(summary = "修改家具工单", description = "更新指定的家具工单信息")
    @PreAuthorize("@ss.hasPermi('find:workOrder:edit')")
    @Log(title = "家具工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FurnitureWorkOrder furnitureWorkOrder)
    {
        return toAjax(furnitureWorkOrderService.updateFurnitureWorkOrder(furnitureWorkOrder));
    }

    /**
     * 删除家具工单
     */
    @Operation(summary = "删除家具工单", description = "批量删除指定的家具工单")
    @PreAuthorize("@ss.hasPermi('find:workOrder:remove')")
    @Log(title = "家具工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(furnitureWorkOrderService.deleteFurnitureWorkOrderByOrderIds(orderIds));
    }

    /**
     * 查询我的购买工单
     */
    @Operation(summary = "查询我的购买工单", description = "获取当前用户的所有购买工单")
    @PreAuthorize("@ss.hasPermi('find:workOrder:myBuy')")
    @GetMapping("/myBuy")
    public TableDataInfo selectMyBuyWorkOrders()
    {
        startPage();
        List<FurnitureWorkOrder> list = furnitureWorkOrderService.selectWorkOrdersByBuyerId(SecurityUtils.getUserId());
        return getDataTable(list);
    }

    /**
     * 查询我的销售工单
     */
    @Operation(summary = "查询我的销售工单", description = "获取当前用户的所有销售工单")
    @PreAuthorize("@ss.hasPermi('find:workOrder:mySell')")
    @GetMapping("/mySell")
    public TableDataInfo selectMySellWorkOrders()
    {
        startPage();
        List<FurnitureWorkOrder> list = furnitureWorkOrderService.selectWorkOrdersBySellerId(SecurityUtils.getUserId());
        return getDataTable(list);
    }

    /**
     * 确认工单
     */
    @Operation(summary = "确认工单", description = "卖家确认工单信息")
    @PreAuthorize("@ss.hasPermi('find:workOrder:confirm')")
    @Log(title = "家具工单", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm/{orderId}")
    public AjaxResult confirmOrder(@PathVariable Long orderId)
    {
        try {
            return toAjax(furnitureWorkOrderService.confirmOrder(orderId));
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 支付工单
     */
    @Operation(summary = "支付工单", description = "买家支付工单款项")
    @PreAuthorize("@ss.hasPermi('find:workOrder:pay')")
    @Log(title = "家具工单", businessType = BusinessType.UPDATE)
    @PutMapping("/pay/{orderId}")
    public AjaxResult payOrder(@PathVariable Long orderId)
    {
        try {
            return toAjax(furnitureWorkOrderService.payOrder(orderId));
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 发货
     */
    @Operation(summary = "发货", description = "卖家标记工单已发货")
    @PreAuthorize("@ss.hasPermi('find:workOrder:ship')")
    @Log(title = "家具工单", businessType = BusinessType.UPDATE)
    @PutMapping("/ship/{orderId}")
    public AjaxResult shipOrder(@PathVariable Long orderId)
    {
        try {
            return toAjax(furnitureWorkOrderService.shipOrder(orderId));
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 确认收货
     */
    @Operation(summary = "确认收货", description = "买家确认收到货物")
    @PreAuthorize("@ss.hasPermi('find:workOrder:receive')")
    @Log(title = "家具工单", businessType = BusinessType.UPDATE)
    @PutMapping("/receive/{orderId}")
    public AjaxResult receiveOrder(@PathVariable Long orderId)
    {
        try {
            return toAjax(furnitureWorkOrderService.receiveOrder(orderId));
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 取消工单
     */
    @Operation(summary = "取消工单", description = "取消指定的工单")
    @PreAuthorize("@ss.hasPermi('find:workOrder:cancel')")
    @Log(title = "家具工单", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{orderId}")
    public AjaxResult cancelOrder(@PathVariable Long orderId)
    {
        try {
            return toAjax(furnitureWorkOrderService.cancelOrder(orderId));
        } catch (RuntimeException e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取工单状态统计
     */
    @Operation(summary = "获取工单状态统计", description = "统计各状态的工单数量")
    @PreAuthorize("@ss.hasPermi('find:workOrder:statistics')")
    @GetMapping("/statistics")
    public AjaxResult getOrderStatistics()
    {
        // 这里可以根据需要实现工单状态统计功能
        // 例如统计各状态的工单数量等
        return AjaxResult.success();
    }
}